package com.ple.app.suggest;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.ple.app.importdata.ImportDataActivity;
import com.ple.app.importdata.MyopenHelper;
import com.ple.app.jzsf.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ManureActivity extends Activity {

    private ListView listView;
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manure);
        /**
         *
         */
        String[] manure_conlm=new String[]{"ID","name","N","P","K","type"};
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        MyopenHelper myopenhelper=new MyopenHelper(ManureActivity.this,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("manure",manure_conlm,null,null,null,null,null,null);
        while(curor.moveToNext()){
            Map<String, Object> item = new HashMap<String, Object>();
            String id=curor.getString(0);
            String name=curor.getString(1);
            String n=curor.getString(2);
            String p=curor.getString(3);
            String k=curor.getString(4);
            item.put("ID", id);
            item.put("name", name);
            item.put("N", n);
            item.put("P", p);
            item.put("K", k);
            list.add(item);}
        curor.close();
        db.close();
        SimpleAdapter adapter = new SimpleAdapter(ManureActivity.this, list, R.layout.manure_item, new String[]{"ID", "name", "N", "P","K"},
                new int[]{R.id.manureID, R.id.manureName, R.id.n, R.id.p,R.id.k});
        listView = (ListView) findViewById(R.id.manure_info); //(ListView) findViewById(R.id.seed_datagrid);
        listView.setAdapter(adapter);
        /**
         *
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String,String> getmap= (HashMap<String, String>) listView.getItemAtPosition(position);
                name=getmap.get("name");
                String ID=getmap.get("ID");
                Intent intent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putString("name",name);
                bundle.putString("ID",ID);
                intent.putExtras(bundle);
                setResult(1,intent);
                finish();
                // Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG).show();
            }
        });

    }//oncreat

}
